/*
 * @Author: andy.dell andy@long.com
 * @Date: 2024-01-07 21:37:44
 * @LastEditors: andy.dell andy@long.com
 * @LastEditTime: 2024-01-07 21:37:44
 * @FilePath: \wasm\logic\snake\src\consts\direction.go
 * @Description:
 */
package consts

import (
	"fmt"
	"strings"
)

// 方向
const (
	DirectionUp    = "w"
	DirectionDown  = "s"
	DirectionLeft  = "a"
	DirectionRight = "d"
)

// 有效的方向
func IsValidDirection(dirLast, dir string) (valid bool) {
	if valid = strings.Contains(fmt.Sprintf("%s%s%s%s", DirectionUp, DirectionDown, DirectionLeft, DirectionRight), strings.ToLower(dir)); !valid {
		return
	}
	valid = (dir == DirectionUp && dirLast != DirectionDown) ||
		(dir == DirectionDown && dirLast != DirectionUp) ||
		(dir == DirectionLeft && dirLast != DirectionRight) ||
		(dir == DirectionRight && dirLast != DirectionLeft)
	return
}
